package com.boot.unity.api.wx.service;

import com.alibaba.fastjson.JSONObject;
import com.boot.unity.api.wx.utils.IpAddress;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class WxCoachservice {

    @Value("${serv.servurl}")
    private String servurl;

    public Object list(Integer starRating, String keyword, Integer schoolId, Integer type, Integer page, Integer limit, String sort, String order) {
        OkHttpClient client = new OkHttpClient();
        String url = servurl+ "/wx/coachs/list?keyword=" + keyword  + "&starRating=" + starRating + "&type=" + type + "&sort=" + sort + "&order=" + order + "&page=" + page + "&limit=" + limit+ "&schoolId=" + schoolId;
        Request request = new Request.Builder()
                .url(url)
                .build();
        String strResult = "";
        try {
            Response response = client.newCall(request).execute();
            strResult = response.body().string();
            if (response.isSuccessful()) {
                // 接口调用成功
                return strResult;
            } else {
                //接口调用失败
                JSONObject resurt = new JSONObject();
                resurt.put("errno", "403");
                resurt.put("errmsg", "接口访问失败");
                return resurt;
            }
        } catch (Exception e) {
            e.printStackTrace();
            JSONObject resurt = new JSONObject();
            resurt.put("errno", "403");
            resurt.put("errmsg", e.getMessage());
            return resurt;
        }
    }

    public Object detail(Integer id) {
        OkHttpClient client = new OkHttpClient();
        String url = servurl+ "/wx/coachs/detail?id=" + id ;
        Request request = new Request.Builder()
                .url(url)
                .build();
        String strResult = "";
        try {
            Response response = client.newCall(request).execute();
            strResult = response.body().string();
            if (response.isSuccessful()) {
                // 接口调用成功
                return strResult;
            } else {
                //接口调用失败
                JSONObject resurt = new JSONObject();
                resurt.put("errno", "403");
                resurt.put("errmsg", "接口访问失败");
                return resurt;
            }
        } catch (Exception e) {
            e.printStackTrace();
            JSONObject resurt = new JSONObject();
            resurt.put("errno", "403");
            resurt.put("errmsg", e.getMessage());
            return resurt;
        }
    }

    public Object listStar(Integer regionId, Integer starRating, String keyword, Integer schoolId, Integer type, Integer page, Integer limit, String sort, String order) {
        OkHttpClient client = new OkHttpClient();
        String url = servurl+ "/wx/coachs/listStar?keyword=" + keyword + "&regionId=" + regionId  + "&starRating=" + starRating + "&type=" + type + "&sort=" + sort + "&order=" + order + "&page=" + page + "&limit=" + limit+ "&schoolId=" + schoolId;
        Request request = new Request.Builder()
                .url(url)
                .build();
        String strResult = "";
        try {
            Response response = client.newCall(request).execute();
            strResult = response.body().string();
            if (response.isSuccessful()) {
                // 接口调用成功
                return strResult;
            } else {
                //接口调用失败
                JSONObject resurt = new JSONObject();
                resurt.put("errno", "403");
                resurt.put("errmsg", "接口访问失败");
                return resurt;
            }
        } catch (Exception e) {
            e.printStackTrace();
            JSONObject resurt = new JSONObject();
            resurt.put("errno", "403");
            resurt.put("errmsg", e.getMessage());
            return resurt;
        }
    }
}
